package com.shf.controller;

import com.github.pagehelper.PageInfo;
import com.shf.entity.Permission;
import com.shf.entity.Role;
import com.shf.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 *权限管理控制层
 * @author 叶枫
 */

@Controller
@RequestMapping("/admin/role/")
public class RoleController {

    //service层属性
    @Autowired
    @Qualifier("rs")
    private RoleService roleService;
    /**
     * 权限展示
     * @param page
     * @param size
     * @param mv
     * @return
     */
    @RequestMapping("findAll")
    public ModelAndView findAllRole(@RequestParam("page") Integer page, @RequestParam("size") Integer size , ModelAndView mv){
        //调用service层方法
       PageInfo<Role> pageInfo =  roleService.findAllRole(page,size);
        //添加公共属性
       mv.addObject("pageInfo",pageInfo);
       //响应
       mv.setViewName("role/role-list");
       return mv;

    }

    /**
     * 添加角色
     * @param role
     * @param
     * @return
     */
    @RequestMapping("saveRole")
    public  String saveRole(Role role){
        role.setId(UUID.randomUUID().toString());
        //调用service层方法
        roleService.saveRole(role);

        return "role/role-add";
    }

    /**
     * 添加权限之前的回显
     */
    @RequestMapping("findRoleByIdAndAllPermission")
    public ModelAndView findRoleByIdAndAllPermission (String id,ModelAndView mv){
        //调用service层方法

        Role role = roleService.findRoleById(id);
      List<Permission> permissionList = roleService.findAllPermission();

        //添加公共属性
        mv.addObject("role",role);
        mv.addObject("permissionList",permissionList);
        //响应
        mv.setViewName("role/role-permission-add");
        return mv;

    }

    /**
     * 添加角色权限
     * @param roleId
     * @param ids
     * @param mv
     * @return
     */

    public String addPermissionToRole( String roleId,String [] ids,ModelAndView mv ){
        //调用service层方法

        roleService.addPermissionToRole(roleId,ids);
        return "redirect:/admin/role/findAll.do?page=1&size=5";
    }

    /**
     * 展示角色详情
     */
    @RequestMapping("findById")
    public ModelAndView findById(String id,ModelAndView mv){
        //调用service层方法
       Role role =  roleService.roleDetail(id);
        List<Permission> permissions = role.getPermissions();
        System.out.println(permissions);
        //添加公共属性
        mv.addObject("role",role);

        //响应
        mv.setViewName("role/role-show");
        return mv;


    }

    /**
     * 根据id删除角色
     * @param id
     * @return
     */
    @RequestMapping("deleteRole")
    public String deleteRole(String id){

        //调用service层
        roleService.deleteRole(id);
        return "redirect:/admin/role/findAll.do?page=1&size=5";
    }


    /**
     * 多选删除
     * @param ids
     * @return
     */
    @RequestMapping("deleteMany")
    public String deleteMany( String  [] ids){

        roleService.deleteMany(ids);
        return "redirect:/admin/role/findAll.do?page=1&size=5";
    }
    @RequestMapping("role-adds")
    public String roleAdds(){
        return "role/role-add";
    }



}

